package com.qqtech.qquan.order.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.coupon.enums.CouponUserStatusDictEnum;
import com.qqtech.qquan.coupon.model.CouponUser;
import com.qqtech.qquan.coupon.model.CouponUserQuery;
import com.qqtech.qquan.coupon.model.CouponUserVo;
import com.qqtech.qquan.coupon.service.CouponUserService;
import com.qqtech.qquan.order.dao.OrderCouponDao;
import com.qqtech.qquan.order.enums.OrderCouponDictStatusEnum;
import com.qqtech.qquan.order.enums.OrderCouponDictTypeEnum;
import com.qqtech.qquan.order.model.*;

@Service
public class OrderCouponServiceImpl extends BaseServiceImpl<OrderCoupon>
		implements OrderCouponService {
	public static final Logger log = LoggerFactory.getLogger(OrderCouponServiceImpl.class);
	
	@Resource
	private OrderCouponDao orderCouponDao;
	
	@Resource
	private CouponUserService couponUserService;

	@Override
	protected BaseDao<OrderCoupon> getBaseDao() {
		return orderCouponDao;
	}

	@Override
	public boolean isToday(Integer userId, OrderCouponDictTypeEnum orderCouponDictTypeEnum) {
		boolean flag=false;
		OrderCouponQuery orderCouponQuery=new OrderCouponQuery();
		orderCouponQuery.setUserId(userId);
		orderCouponQuery.setType(orderCouponDictTypeEnum.getCode());
		orderCouponQuery.setNewAt(TimeUtil.now());
		orderCouponQuery.setStatus(OrderCouponDictStatusEnum.NORMAL.getCode());
		int count=orderCouponDao.queryCount(orderCouponQuery);
		if(count>0){
			flag=true;
		}
		return flag;
	}

	@Override
	public Result handleCancle(String orderNo) {
		OrderCouponQuery orderCouponQuery=new OrderCouponQuery();
		orderCouponQuery.setOrderNo(orderNo);
		List<OrderCoupon> list=orderCouponDao.queryList(orderCouponQuery);
		List<CouponUser> noUses=new ArrayList<CouponUser>();
		List<CouponUser> overdueUses=new ArrayList<CouponUser>();
		CouponUserQuery couponUserQuery=new CouponUserQuery();
		for (OrderCoupon orderCoupon : list) {
			couponUserQuery.setOrderCouponId(orderCoupon.getId());
			List<CouponUserVo> couponUsers1=couponUserService.queryList(couponUserQuery);
			for (CouponUserVo couponUserVo : couponUsers1) {
				if(null!=couponUserVo.getExpireAt()){
					if(TimeUtil.now().before(couponUserVo.getExpireAt())){//未过期的
						couponUserVo.setStatus(CouponUserStatusDictEnum.NOT_USED.getCode());
						noUses.add(couponUserVo);
					}else if(TimeUtil.now().after(couponUserVo.getExpireAt())){//过期
						couponUserVo.setStatus(CouponUserStatusDictEnum.BE_OVERDUE.getCode());
						overdueUses.add(couponUserVo);
					}
				}else{
					couponUserVo.setStatus(CouponUserStatusDictEnum.NOT_USED.getCode());
					noUses.add(couponUserVo);
				}
			}
			orderCoupon.setStatus(OrderCouponDictStatusEnum.CANCLE.getCode());
		}
		orderCouponDao.updateInBatch(list);
		if(noUses.size()>0){
			couponUserService.updateInBatch(noUses);
		}
		if(overdueUses.size()>0){
			couponUserService.updateInBatch(overdueUses);
		}
		return null;
	}


}